package org.esky.study;

/**
 * 
 * @author Jack.Xu
 * @ClassName: CountA
 * @Description: TODO
 * @author: Jack.Xu
 * @date: 2014年3月5日 下午12:00:52
 */
public class CountA {
	public static void main(String[] args) {
		String input = "AAA rating";
		System.out.println(new Iteration().countA(input));
		System.out.println(new RecursiveCall().countA(input));
	}
}

// 迭代（iteration）
class Iteration {
	public int countA(String input) {
		if (input == null || input.length() == 0) {
			return 0;
		}
		int count = 0;
		for (int i = 0; i < input.length(); i++) {
			if (input.substring(i, i + 1).equals("A")) {
				count++;
			}
		}
		return count;
	}
}

// 递归（recursion）
class RecursiveCall {
	public int countA(String input) {
		if (input == null || input.length() == 0) {
			return 0;
		}
		int count = 0;
		if (input.substring(0, 1).equals("A")) {
			count = 1;
		}
		return count + countA(input.substring(1));
	}
}